package com.hzhr.iot.web.controller;

import com.hzhr.iot.core.util.HzhrTypeUtil;
import com.hzhr.iot.user.sdk.model.user_sysInfo.User_SysInfoGetModel;
import com.hzhr.iot.user.sdk.model.user_sysInfo.User_SysInfoModel;
import com.hzhr.iot.user.sdk.model.user_sysInfo.User_SysInfoPutModel;
import com.hzhr.iot.user.sdk.service.User_UserService;
import com.hzhr.iot.user.sdk.service.User_User_SysInfoService;
import com.hzhr.iot.web.controller.base.BaseController;
import com.hzhr.iot.web.model.response.BaseResponse;
import com.hzhr.iot.web.model.response.MapFromPageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@Api(value="公司用户接口",description="公司用户接口")
@RestController
@RequestMapping("/companyUser")
public class CompanyUserController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    User_UserService user_userService;
    @Autowired
    User_User_SysInfoService user_user_sysInfoService;


    @ApiOperation(value = "编辑员工", notes = "编辑员工")
    @ApiImplicitParams({
            @ApiImplicitParam(name ="companyId", value = "公司ID",required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name ="deptId", value = "部门ID",required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name ="name", value = "姓名,最多输入10个字符",required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name ="staffNo", value = "工号,最多输入15个字符",required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name ="gender", value = "性别,1男 2女",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name ="phone", value = "手机号",required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name ="isvalid", value = "启用/停用",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name ="roleId", value = "角色Id",required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name ="id", value = "id",required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "uat", value = "用户登录令牌", dataType = "String", required = true, paramType = "query")}
    )
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    BaseResponse update(@RequestParam(value = "companyId", required = false,defaultValue="-1") String companyId,
                        @RequestParam(value = "deptId", required = false,defaultValue="0") String deptId,
                        @RequestParam(value = "name", required = false,defaultValue="") String name,
                        @RequestParam(value = "staffNo", required = false,defaultValue="") String staffNo,
                        @RequestParam(value = "gender", required = false,defaultValue="0") int gender,
                        @RequestParam(value = "phone", required = false,defaultValue="") String phone,
                        @RequestParam(value = "isvalid", required = false,defaultValue="0") int isvalid,
                        @RequestParam(value = "roleId", required = false,defaultValue="0") String roleId,
                        @RequestParam(value = "id", required = false,defaultValue="0") String id,
                        @RequestParam(value = "uat", required = true) String uat) throws Exception {
        Map<String, String> header = new HashMap<>();
        header.put("token", getToken());
        Map<String, Object> query = new HashMap<>();
        User_SysInfoPutModel body = new User_SysInfoPutModel();
        body.setRealName(name);
        body.setPhone(phone);
        body.setFirstRoleId(HzhrTypeUtil.object2integer(roleId));
        body.setDeptId(HzhrTypeUtil.object2integer(deptId));
        body.setStaffNo(staffNo);
        body.setGender(gender + "");
        user_user_sysInfoService.putByUserId(header, Integer.parseInt(id), query, body);

        return new BaseResponse("0", "成功");
    }

    @ApiOperation(value = "启用/停用员工", notes = "启用/停用员工")
    @ApiImplicitParams({
            @ApiImplicitParam(name ="isvalid", value = "启用/停用",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name ="ids", value = "逗号分隔ID串",required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "uat", value = "用户登录令牌", dataType = "String", required = true, paramType = "query")}
    )
    @RequestMapping(value = "/updateIsvalidByUserId", method = RequestMethod.POST)
    BaseResponse updateIsvalidByUserId(@RequestParam(value = "isvalid", required = false,defaultValue="0") int isvalid,
                                       @RequestParam(value = "ids", required = true,defaultValue="0") String ids,
                                       @RequestParam(value = "uat", required = true) String uat) throws Exception {
        return new BaseResponse("0", "成功");
    }

    @ApiOperation(value = "获取部门下所有员工(包含子部门)", notes = "获取部门下所有员工(包含子部门)")
    @ApiImplicitParams({
            @ApiImplicitParam(name ="companyId", value = "公司ID",required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name ="deptId", value = "部门ID",required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name ="name", value = "姓名",required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name ="staffNo", value = "工号",required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name ="phone", value = "手机号",required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name ="isvalid", value = "启用/停用",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name ="roleId", value = "角色Id",required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name ="begintime", value = "开始时间，形如2018-01-01 23:00:00",required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name ="endtime", value = "结束时间，形如2018-01-01 23:00:00",required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "pageNum", value = "当前页码",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "单页面条数", required =false,dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "uat", value = "用户登录令牌", dataType = "String", required = true, paramType = "query")}
    )
    @RequestMapping(value = "/getList", method = RequestMethod.POST)
    BaseResponse getList(@RequestParam(value = "companyId", required = false,defaultValue="0") String companyId,
                         @RequestParam(value = "deptId", required = false,defaultValue="0") String deptId,
                         @RequestParam(value = "name", required = false,defaultValue="") String name,
                         @RequestParam(value = "staffNo", required = false,defaultValue="") String staffNo,
                         @RequestParam(value = "phone", required = false,defaultValue="") String phone,
                         @RequestParam(value = "isvalid", required = false,defaultValue="-1") int isvalid,
                         @RequestParam(value = "roleId", required = false,defaultValue="0") String roleId,
                         @RequestParam(value = "begintime", required = false,defaultValue="") String begintime,
                         @RequestParam(value = "endtime", required = false,defaultValue="") String endtime,
                         @RequestParam(value ="pageNum", required = false,defaultValue = "1") int pageNum,
                         @RequestParam(value = "pageSize", required = false,defaultValue = "10") int pageSize,
                         @RequestParam(value = "uat", required = true) String uat) throws Exception {
        Map<String, String> header = new HashMap<>();
        header.put("token", getToken());
        Map<String, Object> query = new HashMap<>();
        query.put("show", "list");
        query.put("show_info", true);
        query.put("page", pageNum);
        query.put("count", pageSize);
        List<User_SysInfoModel> user_sysInfoModels =  user_user_sysInfoService.get(header, query).getModelList();

        List<Map<String, Object>> list = new ArrayList<>();
        for (User_SysInfoModel user :
        user_sysInfoModels) {
            Map<String, Object> item = new HashMap<>();
            item.put("deptId", user.getDeptId());
            item.put("deptname", user.getDeptName());
            item.put("id", user.getId());
            item.put("isvalid", 1);
            item.put("phone", user.getPhone());
            item.put("username", user.getUserName());
            item.put("realname", user.getRealName());
            item.put("createtime", user.getCreateTime());
            item.put("gender", HzhrTypeUtil.string2integer(user.getGender()));
            item.put("staffno", user.getStaffNo());
            Map<String, Object> userRole = new HashMap<>();
            userRole.put("name", user.getFirstRoleName());
            userRole.put("roleId", user.getFirstRoleId());
            userRole.put("userId", user.getId());
            item.put("userRole", userRole);
            list.add(item);
        }

        return new BaseResponse("0", "成功", new MapFromPageInfo<>(list));
    }


}
